/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Objects;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/**
 *
 * @author CKCantillas
 */
public class Member {
    private String idNo;
    private String password;
    private String lastName;
    private String firstName;
    private String middleName;
    private String section;
    private String status;
    private String college;
    private String course;
    private String contactNo;
    private String emailAd;
    private String dateOfBirth;

    public Member(String idNo, String password, String lastName, String firstName,
            String middleName, String section, String status, String college,
            String course, String contactNo, String emailAd, String dob) {
        this.idNo = idNo;
        this.password = password;
        this.lastName = lastName;
        this.firstName = firstName;
        this.middleName = middleName;
        this.section = section;
        this.status = status;
        this.college = college;
        this.course = course;
        this.contactNo = contactNo;
        this.emailAd = emailAd;
        this.dateOfBirth = dob;
    }
    
    public Member() {
        
    }
    
    /* Checks the database if a member with memberID is already logged in
     * to the residency by searching the logged_in_members table for a
     * row with member_IDnumber.
     * 
     * Return type: boolean
     * Returns TRUE if a row with memberID exists.
     * Retursn FALSE if a row with memberID does not exist.
     */
    public boolean isLoggedInResidency () {
        DBM.DBManager dbm = new DBM.DBManager();
        
        return dbm.isLoggedInResidency(this.idNo);
    }
    
    /* Gets the current date and time and stores them in Strings,
     * creates a new row in the residency_log table in the database,
     * and then creates another row in the logged_in_members table.
     * 
     * Return type: boolean
     * Returns TRUE if a residency_log entry has been successfully created.
     * Returns FALSE if a residency_log entry is not created.
     */
    public boolean timeIn () {
        DBM.DBManager dbm = new DBM.DBManager();
        ResidencyLog rl = new ResidencyLog();
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        //String datex = dateFormat.format(date);
        //String timeIn = dateFormat.format(cal.getTime());
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String datetime = sdf.format(cal.getTime()).toString();
        
        //rl.setDate(datetime.substring(0, 9));
        //rl.setTimeIn(datetime.substring(11, datetime.length()));
        rl.setMemberID(this.idNo);
        rl.setDate(dateFormat.format(date).substring(0, 9));
        rl.setTimeIn(dateFormat.format(cal.getTime()).substring(11));
        
        if ( dbm.addResidencyLog(rl) ) {
            if ( dbm.addLoggedInMember(dbm.getResidencyLogID(rl), this.idNo) )
                return true;
            else
                System.err.println("Unable to add a row to logged_in_members table.");
        }
        return false;
    }
    
    /* Gets the current time and stores it in a String, updates
     * the logout time of the Member's current residency_log, and then
     * removes the Member from the logged_in_members table.
     * 
     * Return type: boolean
     * Returns TRUE if the residency_log was successfully updated.
     * Returns FALSE if the residency_log was not successfully updated.
     */
    public boolean timeOut() {
        DBM.DBManager dbm = new DBM.DBManager();
        ResidencyLog rl = new ResidencyLog();
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String datetime = sdf.format(cal.getTime()).toString();
        rl.setTimeOut(datetime.substring(11, datetime.length()));
        
        if ( dbm.updateResidencyLog(dbm.getResidencyLoggedInID(this.idNo), 
                rl.getTimeOut()) ) {
            if ( dbm.removeLoggedInMember(dbm.getResidencyLoggedInID(this.idNo)) ) 
                return true;
            else
                System.err.println("Unable to removed member from logged_in_members table.");
        }
        
        return false;
    }
    
    
    /* 
     * 
     * Return type: boolean
     * Returns TRUE if IDnumber and password match a row in member table
     * Returns FALSE otherwise.
     */
    public boolean login() {
        if (new DBM.DBManager().logIn(this.idNo, this.password))
            return true;
        
        return false;
    }
    
    /**
     * @return the idNo
     */
    public String getIdNo() {
        return idNo;
    }

    /**
     * @param idNo the idNo to set
     */
    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the middleName
     */
    public String getMiddleName() {
        return middleName;
    }

    /**
     * @param middleName the middleName to set
     */
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    /**
     * @return the section
     */
    public String getSection() {
        return section;
    }

    /**
     * @param section the section to set
     */
    public void setSection(String section) {
        this.section = section;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * @return the college
     */
    public String getCollege() {
        return college;
    }

    /**
     * @param college the college to set
     */
    public void setCollege(String college) {
        this.college = college;
    }

    /**
     * @return the course
     */
    public String getCourse() {
        return course;
    }

    /**
     * @param course the course to set
     */
    public void setCourse(String course) {
        this.course = course;
    }

    /**
     * @return the contactNo
     */
    public String getContactNo() {
        return contactNo;
    }

    /**
     * @param contactNo the contactNo to set
     */
    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }

    /**
     * @return the emailAd
     */
    public String getEmailAd() {
        return emailAd;
    }

    /**
     * @param emailAd the emailAd to set
     */
    public void setEmailAd(String emailAd) {
        this.emailAd = emailAd;
    }

    /**
     * @return the dateOfBirth
     */
    public String getDateOfBirth() {
        return dateOfBirth;
    }

    /**
     * @param dateOfBirth the dateOfBirth to set
     */
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}